package com.netease.nie.base.utils;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;
import com.netease.nie.base.exceptions.UnexpectedException;

/**
 * 
 * @author KingsHunter
 * @createDate May 30th,2016
 *
 */
public final class JsonUtils {
	
	private static Logger logger = Logger.getLogger(JsonUtils.class);

	public static String obj2String(Object object) {
		if (object != null)
			return JSON.toJSONString(object);
		return null;
	}
	
	public static <T> T getClazz(String text, Class<T> clazz) {
		if (StringUtils.isBlank(text))
			return null;
		try {
			T t = JSON.parseObject(text, clazz);
			return t;
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			throw new UnexpectedException("json格式转换错误");
		}
	}
	
	public static <T> List<T> getList(String text, Class<T> clazz) {
		if (StringUtils.isBlank(text))
			return null;
		return JSON.parseArray(text, clazz);
	}
}
